﻿/*
=======================================================
    StandStatusDetail【状态标准明细】
=======================================================
*/

namespace Services
{
    public interface IStandStatusDetailRepository : IRepository<StandStatusDetail>
    {
        List<StandStatusDetail> GetListByUsingWireropeID(int id);
        List<StandStatusDetail> GetListByUsingWireropeInfo(int hoisterID, int hoisterUsageID, int wireropeTypeID);
    }


    public class StandStatusDetailRepository : BaseRepository<StandStatusDetail>, IStandStatusDetailRepository
    {
        public StandStatusDetailRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<StandStatusDetail> query)
            : base(dB, currentUser, query)
        {
        }

        #region GetSearch
        public override List<StandStatusDetail> GetSearch(Expressionable<StandStatusDetail> where, string orderBy = null, int? top = null)
        {
            var fs = _DB.Queryable<StandStatusDetail, Hoister, HoisterUsage, WireropeType>
                ((x, h, hu, wt) => new JoinQueryInfos(
                    JoinType.Inner, x.HoisterID == h.HoisterID,
                    JoinType.Inner, x.HoisterUsageID == hu.HoisterUsageID,
                    JoinType.Inner, x.WireropeTypeID == wt.WireropeTypeID
                ))
                .Select((x, h, hu, wt) => new StandStatusDetail
                {
                    StandStatusDetailID = x.StandStatusDetailID.SelectAll(),
                    Hoister_Name = h.Name,
                    HoisterUsage_Name = hu.Name,
                    HoisterUsage_IsDelete = hu.IsDelete,
                    WireropeType_Name = wt.Name,
                    WireropeType_IsDelete = wt.IsDelete
                });

            return _query.GetSearch(where, orderBy, top, fs);
        }
        #endregion

        #region GetPage
        public override (List<StandStatusDetail> items, int totalCount) GetPage(Expressionable<StandStatusDetail> where, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var fs = _DB.Queryable<StandStatusDetail, Hoister, HoisterUsage, WireropeType>
              ((x, h, hu, wt) => new JoinQueryInfos(
                  JoinType.Inner, x.HoisterID == h.HoisterID,
                  JoinType.Inner, x.HoisterUsageID == hu.HoisterUsageID,
                  JoinType.Inner, x.WireropeTypeID == wt.WireropeTypeID
              ))
              .Select((x, h, hu, wt) => new StandStatusDetail
              {
                  StandStatusDetailID = x.StandStatusDetailID.SelectAll(),
                  Hoister_Name = h.Name,
                  HoisterUsage_Name = hu.Name,
                  HoisterUsage_IsDelete = hu.IsDelete,
                  WireropeType_Name = wt.Name,
                  WireropeType_IsDelete = wt.IsDelete
              });

            return _query.GetPage(where, pageIndex, pageSize, orderBy, fs);
        }
        #endregion

        #region 根据在用绳获取相关的标准数据
        public List<StandStatusDetail> GetListByUsingWireropeID(int id)
        {
            var uw = _DB.Queryable<UsingWirerope>().InSingle(id);

            return GetListByUsingWireropeInfo(uw.HoisterID, uw.HoisterUsageID, uw.WireropeTypeID);
        }
        #endregion

        #region 根据提升机、提升用途、钢丝绳类型获取相关的标准数据
        public List<StandStatusDetail> GetListByUsingWireropeInfo(int hoisterID, int hoisterUsageID, int wireropeTypeID)
        {
            return _DB.Queryable<StandStatusDetail, StandStatus>((x, ss) => new JoinQueryInfos(JoinType.Inner, x.StandStatusID == ss.StandStatusID))
                .Select((x, ss) => new StandStatusDetail { StandStatusDetailID = x.StandStatusDetailID.SelectAll(), StandStatus_Type = ss.Type })
                .Where(x => x.HoisterID == hoisterID && x.HoisterUsageID == hoisterUsageID && x.WireropeTypeID == wireropeTypeID)
                .ToList();
        }
        #endregion
    }
}
